package com.gaohe.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.gaohe.common.R;
import com.gaohe.pojo.Notice;
import com.gaohe.service.NoticeService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/notice")
@Slf4j
public class NoticeController {
    @Autowired
    private NoticeService noticeService;
    @GetMapping("/page")
    public R page(@RequestParam int page,
                  @RequestParam int pageSize,
                  @RequestParam(required = false) String name
                  ){
        Page<Notice> noticePage = new Page<>(page, pageSize);
        LambdaQueryWrapper<Notice> qw = new LambdaQueryWrapper<>();
        qw.like(!StringUtils.isEmpty(name),Notice::getTitle,name);
        qw.orderByDesc(Notice::getUpdateTime);
        noticeService.page(noticePage,qw);
        return R.success(noticePage);
    }
    //通知修改
    //回显
    @GetMapping("/{id}")
    public R findByid(@PathVariable Long id){
        return R.success(noticeService.getById(id));
    }
    //修改
    @PutMapping
    public R update(@RequestBody Notice notice){
        boolean b = noticeService.updateById(notice);
        return b?R.success("修改成功"):R.error("修改失败");
    }
    //添加
    @PostMapping
    public R save(@RequestBody Notice notice)
    {
        boolean save = noticeService.save(notice);
        return save?R.success("添加成功"):R.error("添加失败");
    }
    //删除
    @DeleteMapping
    public R delete(@RequestParam Long id){
        boolean b = noticeService.removeById(id);
        return b?R.success("删除成功"):R.error("删除失败");
    }
    @GetMapping
    public R findAll(){
        List<Notice> list = noticeService.list();
        return R.success(list);
    }

}
